package com.qq2008.game.mojie.jdo;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.qq2008.game.MojieApplication;
import com.qq2008.game.mojie.server.RoleServer;
import com.qq2008.game.mojie.service.RoleService;
import com.qq2008.game.mojie.utils.CommonUtil;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

// 玩家
@TableName("role")
@Data
public class Role extends com.qq2008.game.mojie.jdo.Player {

    // 编号
    private int userId = 0;

    // 当前经验
    private int exp = 0;

    // 当前金币
    private Integer money;

    // 升级所需经验
    @TableField(exist = false)
    private int maxExp = 0;


    /***
     * 增加经验
     * @param addExp
     */
    public void addExp(int addExp,int addMoney){
        this.money += addMoney;
        if(addExp > 0){
            this.exp += addExp;
        }
        if(this.exp >= this.maxExp){
            this.upLv();
        }
        this.updateLevel();
    }


    /***
     * 升级
     */
    public void upLv(){
        this.exp -= this.maxExp;
        this.level += 1;
        this.maxExp = CommonUtil.calcUpExp(this.level);
        this.minAttack = this.level * 10 + 20;
        this.maxAttack = this.level * 10 + 60;
        this.minDefence = this.level * 8 + 15;
        this.maxDefence = this.level * 8 + 45;
    }

    public void updateLevel(){
        //升级信息写入数据库
        Role role = new Role();
        role.setExp(this.exp);
        role.setLevel(this.level);
        role.setMaxExp(this.maxExp);
        role.setMinAttack(this.minAttack);
        role.setMaxAttack(this.maxAttack);
        role.setMinDefence(this.minDefence);
        role.setMaxDefence(this.maxDefence);
        role.setUserId(this.userId);
        role.setMoney(this.money);
        MojieApplication.context.getBean(RoleService.class).updateByUserId(role);
    }


}
